//
//  ConIntegralController.m
//  Shengzhuli
//
//  Created by FXGuo on 17/3/26.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConIntegralController.h"
#import "BillDetailCell.h"
#import "IntegralsCell.h"
#import "SZLAccountHelper.h"
#import "PersonNetWork.h"
#import "MBProgressHUD+MJ.h"
#import "ConActivityDetailController.h"

@interface ConIntegralController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,assign) NSInteger integral;//积分
@property (nonatomic,assign) NSInteger curPage;
@property (nonatomic,strong) NSMutableArray *sources;

@end

@implementation ConIntegralController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的积分";
    [self setTableView];
    [self getIntegral];
    self.curPage = 1;
    [self getIntegralDetail];
}

- (void)setTableView
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, ScreenW, ScreenH-64) style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.sectionHeaderHeight = 0.01;
    tableView.sectionFooterHeight = 0.01;
    self.tableView = tableView;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BillDetailCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([BillDetailCell class])];
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([IntegralsCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([IntegralsCell class])];
    [self.view addSubview:tableView];
    tableView.tableFooterView = [[UIView alloc]init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 125;
    }else if (indexPath.row == 0){
        return 40;
    }
    return 70;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return self.sources.count+1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        IntegralsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([IntegralsCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.integral = self.integral;
        __WEAK
        cell.integralClick = ^{
            ConActivityDetailController *webVc = [[ConActivityDetailController alloc]init];
            webVc.type = WebTypeIntegralRule;
            [weakSelf.navigationController pushViewController:webVc animated:YES];
        };
        return cell;
    }
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"积分明细";
        return cell;
    }
    BillDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BillDetailCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.sources[indexPath.row-1];
    cell.isScore = YES;
    return cell;
}
- (void)getIntegral
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    [self showClearBgWaiter];
    [PersonNetWork getIntegralWithParame:parame response:^(BOOL success, NSString *msg,NSInteger integral) {
        [self hideWaiter];
        if (success) {
            self.integral = integral;
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)getIntegralDetail
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"pageNo"] = @(self.curPage);
    [self showClearBgWaiter];
    [PersonNetWork getIntegralDetailWithParame:parame response:^(BOOL success, NSString *msg,NSArray *integrals) {
        [self hideWaiter];
        if (success) {
            [self.sources addObjectsFromArray:integrals];
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}


@end
